package com.reflect;

import java.lang.reflect.Field;

public class FiledAccessorTest {

    public static void main(String[] args) throws Exception {
        MyClass myClass = new MyClass();

        MyClass myClass2 = new MyClass();
        myClass2.setMyField("xxxxxxxxxx");

        Field field = myClass.getClass().getDeclaredField("myField");
        field.setAccessible(true);

        field.set(myClass, "New Value");

        Object o = field.get(myClass2);
        System.out.println(o);

        System.out.println(myClass.getMyField());
    }

    static class MyClass {
        private String myField = "Old Value";

        public void setMyField(String myField) {
            this.myField = myField;
        }

        public String getMyField() {
            return myField;
        }
    }

}
